import { Composite } from "../core/Composite";
import { Tick } from "../core/Tick";
import { ActionStatus } from "../core/BaseNode";

export class MemSequence extends Composite {

    constructor(params?: any) {
        super(params);
        this.name = 'MemSequence';
    }

    open(tick: Tick) {
        tick.blackboard.set('runningChild', 0, tick.tree.id, this.id);
    }

    tick(tick: Tick) : ActionStatus {
        let child = tick.blackboard.get('runningChild', tick.tree.id, this.id);
        for (let i = child; i < this.children.length; i++) {
            let status = this.children[i]._execute(tick);
            if (status !== ActionStatus.SUCCESS) {
                if (status === ActionStatus.RUNNING) {
                    tick.blackboard.set('runningChild', i, tick.tree.id, this.id);
                }
                return status;
            }
        }
        return ActionStatus.SUCCESS;
    }

}